// OpenGL Display by Glenn Fiedler <gaffer@gaffer.org>
// This code is in the public domain, so please use it however you like!

#define FULLSCREEN

#include "Display.h"

int main( int argc, char * argv[] )
{
	#ifdef FULLSCREEN
	const int DisplayHeight = GetDisplayHeight();
	const int DisplayWidth = GetDisplayWidth();
	#else
	const int DisplayWidth = 800;
	const int DisplayHeight = 600;
	#endif

	if ( !OpenDisplay( "OpenGL Display", DisplayWidth, DisplayHeight ) )
	{
		fprintf( stderr, "failed to open display!\n" );
		return 1;
	}
	
	#ifdef FULLSCREEN
	HideMouseCursor();
	#endif
	
	glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 40.0f, (float) DisplayWidth / DisplayHeight, 0.1f, 100.0f );

	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	gluLookAt( 0, 6, 0,
	           0, 0, 0,
			   0, 0, 1 );

	glShadeModel( GL_SMOOTH );
	glEnable( GL_LIGHT0 );
	GLfloat lightAmbientColor[] = { 0.5, 0.5, 0.5, 1.0 };
	glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lightAmbientColor );
	glLightf( GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.01f );
	GLfloat position[4] = { 10, 10, 10, 1 };
	glLightfv( GL_LIGHT0, GL_POSITION, position );
	glEnable( GL_LIGHTING );

    GLfloat color[] = { 0.05f, 0.1f, 1.0f, 0.0f };
	glMaterialfv( GL_FRONT, GL_AMBIENT, color );
	glMaterialfv( GL_FRONT, GL_DIFFUSE, color );

	glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );

    glEnable( GL_CULL_FACE );
    glCullFace( GL_BACK );
    glFrontFace( GL_CW );

	while ( true )
	{
		Input input = Input::Sample();

		if ( input.escape )
			break;

		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );		

		glRotatef( 0.5f, -0.5f, 0.1f, 0.25f );
	
		glBegin( GL_QUADS );

			glNormal3f(  0,  0, +1 );
			glVertex3f( -1, -1, +1 );
			glVertex3f( -1, +1, +1 );
			glVertex3f( +1, +1, +1 );
			glVertex3f( +1, -1, +1 );

			glNormal3f(  0,  0, -1 );
			glVertex3f( -1, -1, -1 );
			glVertex3f( +1, -1, -1 );
			glVertex3f( +1, +1, -1 );
			glVertex3f( -1, +1, -1 );

			glNormal3f(  0, +1,  0 );
			glVertex3f( -1, +1, -1 );
			glVertex3f( +1, +1, -1 );
			glVertex3f( +1, +1, +1 );
			glVertex3f( -1, +1, +1 );

			glNormal3f(  0, -1,  0 );
			glVertex3f( -1, -1, -1 );
			glVertex3f( -1, -1, +1 );
			glVertex3f( +1, -1, +1 );
			glVertex3f( +1, -1, -1 );

			glNormal3f( +1,  0,  0 );
			glVertex3f( +1, -1, -1 );
			glVertex3f( +1, -1, +1 );
			glVertex3f( +1, +1, +1 );
			glVertex3f( +1, +1, -1 );

			glNormal3f( -1,  0,  0 );
			glVertex3f( -1, -1, -1 );
			glVertex3f( -1, +1, -1 );
			glVertex3f( -1, +1, +1 );
			glVertex3f( -1, -1, +1 );

		glEnd();

		UpdateDisplay();
	}

	CloseDisplay();
	
	#ifdef FULLSCREEN
	ShowMouseCursor();
	#endif

	return 0;
}
